package com.yaowen.edittext;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GridActivity extends AppCompatActivity {
    ListView listView;
    private List<Map<String,Object>> data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid);
        listView= (ListView) findViewById(R.id.listView);
        data=getData();
        MyAdapter adapter = new MyAdapter(this);
        listView.setAdapter(adapter);
    }

    private List<Map<String, Object>> getData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map;
        for (int i = 0; i <= 10; i++) {
            map = new HashMap<String, Object>();
            map.put("imageView", R.mipmap.ic_launcher);
            map.put("imageView2", R.mipmap.ic_launcher);
            map.put("imageView3", R.mipmap.ic_launcher);
            map.put("title", "乒乓球");
            map.put("title2", "足球");
            map.put("info", "快乐源于运动......");
            map.put("info2", "生命在于运动......");
            list.add(map);
        }
        return list;
    }
    //ViewHolder静态类
    class ViewHolder1{
        public ImageView imageView;
        public TextView title;
        public TextView info;

    }
    //各个布局的控件资源

    class ViewHolder2{
        public ImageView imageView2;
        public TextView title2;
        public TextView info2;
    }
    class ViewHolder3{
        public ImageView imageView3;

    }

    public class MyAdapter extends BaseAdapter {
        private LayoutInflater mInflater = null;
        final int VIEW_TYPE = 3;
        final int TYPE_1 = 0;
        final int TYPE_2 = 1;
        final int TYPE_3 = 2;
        //构建一个函数使用context上下文；
        private MyAdapter(Context context){
            //根据context上下文加载布局，这里的是MainActivity本身，即this
            this.mInflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            //在此适配器中所代表的数据集中的条目数
            return data.size();
        }
        //每个convert view都会调用此方法，获得当前所需要的view样式
        @Override
        public int getItemViewType(int position) {
            // TODO Auto-generated method stub
            int p = position%6;
            if(p == 0)
                return TYPE_1;
            else if(p < 3)
                return TYPE_2;
            else if(p < 6)
                return TYPE_3;
            else
                return TYPE_1;

        }
        @Override
        public int getViewTypeCount() {
            // TODO Auto-generated method stub
            return 3;
        }
        @Override
        public Object getItem(int position) {
            //获取数据集中与指定索引对应的数据项
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            //获取在列表中与指定索引对应的行id
            return position;
        }

        //获取一个在数据集中指定索引的视图来显示数据
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder1 holder1 = null;
            ViewHolder2 holder2=null;
            ViewHolder3 holder3=null;
            int type = getItemViewType(position);
            //有convertView，按样式，取得不用的布局
            if(convertView == null)
            {
                Log.e("convertView = ", " NULL");

                //按当前所需的样式，确定new的布局
                switch(type)
                {
                    case TYPE_1: {
                        convertView = mInflater.inflate(R.layout.listview_item, parent, false);
                        //convertView=mInflater.inflate(R.layout.list_item,null);
                        holder1 = new ViewHolder1();
                        //holder1.imageView = (ImageView)convertView.findViewById(R.id.imageView);
                        holder1.title = (TextView) convertView.findViewById(R.id.listview_text1);
                        holder1.info = (TextView) convertView.findViewById(R.id.listview_text2);
                        Log.e("convertView = ", "NULL TYPE_1");
                        convertView.setTag(holder1);
                        break;
                    }
                   /* case TYPE_2:
                        convertView = mInflater.inflate(R.layout.listview_item, parent, false);
                        holder2 = new ViewHolder2();
                       // holder2.imageView2 = (ImageView)convertView.findViewById(R.id.imageView2);
                        holder2.title2= (TextView) convertView.findViewById(R.id.listview_text1);
                        holder2.info2= (TextView) convertView.findViewById(R.id.listview_text2);
                        Log.e("convertView = ", "NULL TYPE_2");
                        convertView.setTag(holder2);
                        break;
                    case TYPE_3:
                        convertView = mInflater.inflate(R.layout.listview_item, parent, false);
                        holder3 = new ViewHolder3();
                        //holder3.imageView3 = (ImageView)convertView.findViewById(R.id.imageView3);
                        Log.e("convertView = ", "NULL TYPE_3");
                        convertView.setTag(holder3);
                        break;*/
                    default:
                        break;
                }
            }
            else
            {
                //有convertView，按样式，取得不用的布局
                switch(type)
                {
                    case TYPE_1:
                        holder1 = (ViewHolder1) convertView.getTag();
                        Log.e("convertView !!!!!!= ", "NULL TYPE_1");
                        break;
                /*    case TYPE_2:
                        holder2 = (ViewHolder2) convertView.getTag();
                        Log.e("convertView !!!!!!= ", "NULL TYPE_2");
                        break;
                    case TYPE_3:
                        holder3 = (ViewHolder3) convertView.getTag();
                        Log.e("convertView !!!!!!= ", "NULL TYPE_3");
                        break;*/
                }
            }

            //设置资源
            switch(type)
            {
                case TYPE_1:
                    holder1.imageView.setBackgroundResource(R.mipmap.ic_launcher);
                    holder1.title.setText((String) data.get(position).get("title"));
                    holder1.info.setText((String) data.get(position).get("info"));
                    break;
               /* case TYPE_2:
                    holder2.imageView2.setBackgroundResource(R.mipmap.ic_launcher);
                    holder2.title2.setText((String) data.get(position).get("title2"));
                    holder2.info2.setText((String) data.get(position).get("info2"));
                    break;
                case TYPE_3:
                    holder3.imageView3.setBackgroundResource(R.mipmap.ic_launcher);
                    break;*/
            }
            return convertView;
        }
    }

}
